/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2022-11-16
 * Time: 22:39
 */
public class test {
    // 2. 方法传参：形参为父类型引用，可以接收任意子类的对象
    public static void func(animal qq){
        qq.eat();
    }
    // 3. 作返回值：返回任意子类对象
    public static animal func1(String ww){
        if("狗".equals(ww)){
            return new animal("狗狗",1);
        }else if("猫".equals(ww)){
            return new animal("猫猫",1);
        }else
            return null;
    }
    public static void main(String[] args) {
//animal  a1=new cat("小猪",18);// 1. 直接赋值：子类对象赋值给父类对象
//dog  b1=new dog("小六",6);
//func(a1);
//func(b1);
//animal w1=func1("狗");
//w1.eat();
//w1=func1("猫");
//w1.eat();
        // 编译失败，编译时编译器将a当成Animal对象处理
        // 向上转型
        // 程序可以通过编程，但运行时抛出异常---因为：a实际指向的是狗
        // 现在要强制还原为猫，无法正常还原，运行时抛出：ClassCastException
        animal a=new dog("小猪",6);
        if(a instanceof cat){
        cat cat1=new cat("小猫",9);
        cat1=(cat)a;}

    }
}
class animal{
    String name;
    int age;
    public animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void eat(){
        System.out.println(name+"吃饭");
    }
}
class cat extends animal{
    public cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println(name+"吃鱼");
    }
}
class dog extends animal{
    public dog(String name, int age) {
        super(name, age);
    }
    @Override
    public void eat() {
        System.out.println(name+"吃骨头");
    }
}
